class User
  include DataMapper::Resource

  def self.default_repository_name; :datastore end
  property :id, Serial
  property :name, String
  property :email, String
  property :domain, String
  property :password, String
  property :created_at, DateTime
  property :updated_at, DateTime

  has n, :events

  before :save do
    self.updated_at = Time.now
  end

end


class Event
  include DataMapper::Resource

  def self.default_repository_name; :datastore end
  property :id, Serial
  property :user_id, Integer
  property :name, String
  property :csv_header, Text
  property :created_at, DateTime
  property :updated_at, DateTime

  belongs_to :user
  has n, :visitors
  has n, :seminors

  before :save do
    self.created_at = Time.now
  end

end

class Seminor
  include DataMapper::Resource

  def self.default_repository_name; :datastore end
  property :id, Serial
  property :event_id, Integer
  property :name, String
  property :created_at, DateTime
  property :updated_at, DateTime

  belongs_to :event
  has n, :participants

  before :save do
    self.created_at = Time.now
  end

end

class Participant
  include DataMapper::Resource

  def self.default_repository_name; :datastore end
  property :id, Serial
  property :seminor_id, Integer
  property :visitor_id, Integer
  property :came_at, DateTime
  property :created_at, DateTime
  property :updated_at, DateTime

  belongs_to :visitor
  belongs_to :seminor

  before :save do
    self.created_at = Time.now
  end
end

class Visitor
  include DataMapper::Resource

  def self.default_repository_name; :datastore end
  property :id, Serial
  property :event_id, Integer
  property :csv, Text
  property :name, String
  property :mail, String
  property :number, String
  property :user_hash, String
  property :came_at, DateTime
  property :created_at, DateTime
  property :updated_at, DateTime

  belongs_to :event
  has n, :participants

  before :save do
    self.created_at = Time.now
  end

  # user_hashを元にQRコード表示用URL文字列を返す
  def qr_code
    return "" if self.user_hash.nil? or self.user_hash.empty?
    base_url = "http://chart.apis.google.com/chart?cht=qr&choe=UTF-8&chs=150x150&chl="
    return base_url + self.user_hash
  end
  
end

